 /*****************************************************************************
 *  This file is part of the OpenDomo project.
 *  Copyright(C) 2011 OpenDomo Services SL
 *  
 *  Daniel Lerch Hostalot <dlerch@opendomo.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *****************************************************************************/

// ----------------------------------------------------------------------------
// author: Daniel Lerch <dlerch@gmail.com>, http://www.opendomo.com
// date: APRIL 2009
//
// CHANGES:
//
// ----------------------------------------------------------------------------


#ifndef __HASHTABLE_H__
#define __HASHTABLE_H__


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

unsigned int hash(char *key, size_t ht_size)
{
   unsigned int h;
   unsigned char *p;

   h=0;
   for(p=(unsigned char*)key; *p!='\0'; p++)
      h = 31*h + *p;

   return h%ht_size;
}

#define HASHTABLE_CREATE(varname,tablesize,type) \
   typedef struct __##varname##_node \
   { \
      char *key; \
      type value; \
      struct __##varname##_node *next; \
   } \
   _##varname##_node; \
   \
   typedef struct __##varname \
   { \
      _##varname##_node *ht[tablesize]; \
      size_t size; \
   } \
   _##varname; \
   _##varname *varname = malloc(sizeof(_##varname)); \
   varname->size = tablesize; \
   { \
      int i; \
      for(i=0; i<tablesize; i++) \
         varname->ht[i]=NULL; \
   }
 

#define HASHTABLE_PTR(varname,tablesize, type) \
   typedef struct __##varname##_node \
   { \
      char *key; \
      type value; \
      struct __##varname##_node *next; \
   } \
   _##varname##_node; \
   \
   typedef struct __##varname \
   { \
      _##varname##_node *ht[tablesize]; \
      size_t size; \
   } \
   _##varname; \
  _##varname *varname;



#define HASHTABLE_SET(varname,varkey,varvalue) \
   { \
      _##varname##_node *n = NULL; \
      int found = 0; \
      unsigned int h = hash(varkey, (varname)->size); \
      for(n=(varname)->ht[h]; n!=NULL; n=n->next) \
      { \
         if(strcmp(varkey, n->key)==0) \
         { \
            n->value = varvalue; \
            found = 1; \
         } \
      } \
      if(!found) \
      { \
         n = malloc(sizeof(_##varname##_node)); \
         n->key = varkey; \
         n->value = varvalue; \
         n->next = varname->ht[h]; \
         varname->ht[h] = n; \
      } \
   }


#define HASHTABLE_GET(varname,varkey,varvalue) \
   { \
      _##varname##_node *n = NULL; \
      unsigned int h = hash(varkey, (varname)->size); \
      for(n=varname->ht[h]; n!=NULL; n=n->next) \
      if(strcmp(varkey, n->key)==0) \
         varvalue = n->value; \
   }


#define HASHTABLE_DESTROY(varname) \
   { \
      int i; \
      for(i=0; i<(varname)->size; i++) \
      { \
         _##varname##_node *prev = NULL; \
         _##varname##_node *n = NULL; \
         for(n=varname->ht[i]; n!=NULL; n=n->next) \
         { \
            if(prev) \
               free(prev); \
            prev = n; \
         } \
         if(prev) \
            free(prev); \
      } \
      if(varname); \
         free(varname); \
   }



#endif


